package com.meihua.baoleyi.utils;

import android.content.Context;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import com.meihua.baoleyi.R;


/**
 * ClassName：liming
 * Times: 2017/10/16
 */

public class CountDownTimerUtils extends CountDownTimer {
    private TextView mTextView;
    private Context mContext;

    public CountDownTimerUtils(Context context, TextView textView, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.mTextView = textView;
        this.mContext = context;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        mTextView.setClickable(false);
        mTextView.setText(millisUntilFinished / 1000 + "秒后重发");
        mTextView.setBackgroundResource(R.drawable.shape_code_btn_press);
        SpannableString spannableString = new SpannableString(mTextView.getText().toString());
        ForegroundColorSpan span = new ForegroundColorSpan(mContext.getResources().getColor(R.color.black));
        spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        mTextView.setText(spannableString);
        mTextView.setTextColor(mContext.getResources().getColor(R.color.black));
    }

    @Override
    public void onFinish() {
        mTextView.setText("获取验证码");
        mTextView.setClickable(true);
        mTextView.setBackgroundResource(R.drawable.bg_round_green);
        mTextView.setTextColor(mContext.getResources().getColor(R.color.white));
    }
}
